//
//  FileManager.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/4/26.
//

import Foundation

public extension FileManager {
    
    func jsonFromFile(
            atPath path: String,
            readingOptions: JSONSerialization.ReadingOptions = .allowFragments) throws -> [String: Any]? {
    
        let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
        let json = try JSONSerialization.jsonObject(with: data, options: readingOptions)
    
        return json as? [String: Any]
    }
    
    func jsonFromFile(
            withFilename filename: String,
            at bundleClass: AnyClass? = nil,
            readingOptions: JSONSerialization.ReadingOptions = .allowFragments) throws -> [String: Any]? {
        // https://stackoverflow.com/questions/24410881/reading-in-a-json-file-using-swift
    
        let name = filename.components(separatedBy: ".")[0]
        let bundle = bundleClass != nil ? Bundle(for: bundleClass!) : Bundle.main
    
        if let path = bundle.path(forResource: name, ofType: "json") {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
            let json = try JSONSerialization.jsonObject(with: data, options: readingOptions)
    
            return json as? [String: Any]
        }
    
        return nil
    }
}
